package com.art.sunflower.service.impl;

import com.art.sunflower.exception.NullClassroomException;
import com.art.sunflower.mapper.ClassroomMapper;
import com.art.sunflower.pojo.Classroom;
import com.art.sunflower.pojo.Lay;
import com.art.sunflower.service.ClassroomService;
import com.art.sunflower.util.MyPageInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ClassroomServiceImpl implements ClassroomService {

    @Resource
    ClassroomMapper classroomMapper;

    @Override
    public MyPageInfo getList(Integer pageSize, Integer pageNo) throws NullClassroomException {
        PageHelper.startPage(pageNo, pageSize);
        List<Map<String, Object>> classroom = classroomMapper.selectList();
        if (classroom.size() == 0)
            throw new NullClassroomException();
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(classroom);
        return new MyPageInfo(pageInfo);
    }

    @Override
    public void add(Integer id, String name, String grade, String message, Integer num) {
        classroomMapper.insertClassroom(id, name, grade, message, num);
    }

    @Override
    public void update(Integer id, Integer teacherId, String name, String grade, String message, Integer num) throws NullClassroomException {
        Map<String, Object> classroom = classroomMapper.selectClassroom(id);
        if (classroom == null) {
            throw new NullClassroomException();
        } else {
            classroomMapper.updateStudent(id, teacherId, name, grade, message, num);
        }
    }

    @Override
    public void delete(Integer id) throws NullClassroomException {
        Map<String, Object> classroom = classroomMapper.selectClassroom(id);
        if (classroom == null) {
            throw new NullClassroomException();
        } else {
            classroomMapper.delete(id);
        }
    }

    @Override
    public Map<String, Object> getById(Integer id) throws NullClassroomException {
        Map<String, Object> classroom = classroomMapper.selectClassroom(id);
        if (classroom == null) {
            throw new NullClassroomException();
        } else {
            return classroomMapper.selectById(id);
        }
    }

    @Override
    public int insert(Classroom classroom) {
        return classroomMapper.insert(classroom);
    }

    @Override
    public List<Classroom> selectAll() {
        return classroomMapper.selectAll();
    }

    @Override
    public int selectOneClassroom(int id) {
        return classroomMapper.selectOneClassroom(id);
    }

    @Override
    public Lay findClassroomByTeacher(String teacherName, Integer pagenum, Integer pagesize) {
        //需要进行分页
        PageHelper.startPage(pagenum, pagesize);
        List<Map<String, Object>> classroomList3 = (List<Map<String, Object>>) classroomMapper.findClassroomByTeacher(teacherName, pagenum, pagesize);
        PageInfo info = new PageInfo(classroomList3);//创建pageinfo，包含分页的信息
        Lay lay2 = new Lay();
        lay2.setPagenum(pagenum);
        lay2.setCount(info.getTotal());//总条数
        lay2.setData(info.getList());//显示的数据
        return lay2;
    }
}
